package com.wangijun.yuan.web.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangijun.yuan.web.base.CommonPage;
import com.wangijun.yuan.web.base.CommonResult;
import com.wangijun.yuan.core.domain.TopicInfo;
import com.wangijun.yuan.web.mapper.TopicInfoMapper;
import com.wangijun.yuan.web.model.dto.PersonalTopicParam;
import com.wangijun.yuan.web.model.dto.QryTopicParam;
import com.wangijun.yuan.web.model.dto.RankingTopicQryParam;
import com.wangijun.yuan.web.model.dto.TopicListParam;
import com.wangijun.yuan.web.model.vo.TopicInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class TopicInfoService extends ServiceImpl<TopicInfoMapper, TopicInfo> {


    private final OperateInfoService operateInfoService;

    /**
     * 功能描述: 标签分页查询
     *
     * @author wlf 2024/7/15 16:25
     */
    public CommonPage<TopicInfoVO> topicQryList(TopicListParam param) {
        Page<TopicInfo> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(param.getText()), TopicInfo::getName, param.getText())
                .page(param.page());
        List<Long> followTopicIds = operateInfoService.getFollowTopicIds();
        return CommonPage.toPage(page, TopicInfoVO.class, (v) -> v.setFollowFlag(followTopicIds.contains(v.getId())));
    }

    public TopicInfoVO get(Long id) {
        if (id == null) {
            CommonResult.error("ID不可为空");
        }
        TopicInfo tagInfo = this.getById(id);
        if (tagInfo == null) {
            CommonResult.error("话题信息不存在");
        }
        TopicInfoVO tagInfoVO = BeanUtil.copyProperties(tagInfo, TopicInfoVO.class);
        List<Long> followTopicIds = operateInfoService.getFollowTopicIds();
        tagInfoVO.setFollowFlag(followTopicIds.contains(tagInfoVO.getId()));
        return tagInfoVO;
    }


    /**
     * 功能描述: 作者用户热度榜单
     *
     * @author wlf 2024/7/21 9:28
     */
    public CommonPage<TopicInfoVO> rankingTopic(RankingTopicQryParam param) {
        LambdaQueryChainWrapper<TopicInfo> wrapper = this.lambdaQuery();
        wrapper.orderByDesc(TopicInfo::getReadNum);
        Page<TopicInfo> page = wrapper.page(param.page());
        List<Long> followTopicIds = operateInfoService.getFollowTopicIds();
        return CommonPage.toPage(page, TopicInfoVO.class, (vo) -> vo.setFollowFlag(followTopicIds.contains(vo.getId())));
    }

    public CommonPage<TopicInfoVO> followTopic(PersonalTopicParam param) {
        List<Long> followTopicIds = operateInfoService.getFollowTopicIds(param.getUserId());
        if (followTopicIds.isEmpty()) {
            return CommonPage.toPage(param.page(), TopicInfoVO.class);
        }

        Page<TopicInfo> page = this.lambdaQuery()
                .in(TopicInfo::getId, followTopicIds)
                .like(StrUtil.isNotBlank(param.getText()), TopicInfo::getName, param.getText())
                .orderByDesc(TopicInfo::getReadNum)
                .page(param.page());
        return CommonPage.toPage(page, TopicInfoVO.class);
    }

    public CommonPage<TopicInfoVO> qryTopic(QryTopicParam param) {
        Page<TopicInfo> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(param.getText()), TopicInfo::getName, param.getText())
                .orderByDesc(TopicInfo::getReadNum)
                .page(param.page());
        List<Long> followTopicIds = operateInfoService.getFollowTopicIds();
        return CommonPage.toPage(page, TopicInfoVO.class, (vo) -> vo.setFollowFlag(followTopicIds.contains(vo.getId())));
    }
}
